package com.ufaud.ais.sys.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.ufaud.ais.pub.entity.BaseEntity;

/**
 * 
 * @author chenjd
 * 
 */

@Entity
@Table(name = "employee")
@NamedQueries ({
    @NamedQuery(
        name = "findUserByName",
        query = "select u from Employee u" +
        		" left outer join fetch u.department" +
        		" left outer join fetch u.roleSet" +
        		" where u.accountName = :username "
        ),
    @NamedQuery(
        name = "findUserByDeptId",
        query = "select e from Employee e where e.department.deptId = :deptId ")
})
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Employee extends BaseEntity<Long> {

	@Id
	@GeneratedValue
	@Column(name = "employee_id")
	private Long employeeId;

	@Column(name = "employee_code")
	private String employeeCode;

	@Column(name = "employee_name")
	private String employeeName;

	private String email;

	@Column(name = "employ_date")
	private Date employDate;

	private String job;

	@ManyToOne
	@JoinColumn(name="dept_id")
	private Department department;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "employee_role_relation", joinColumns = @JoinColumn(name = "employee_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	private Set<Role> roleSet = new HashSet<Role>();

	@OneToMany(mappedBy = "employee")
	private Set<TransferRecord> transferRecordSet;

	// -- 账户信息 --
	@Column(name = "account_name")
	private String accountName;
	@Column(name = "account_password")
	private String accountPassword;
	@Column(name="account_enabled")
    private boolean enabled = true;
	@Column(name="account_expired")
    private boolean accountExpired;
	@Column(name="account_locked")
    private boolean accountLocked;
	@Column(name="credentials_expired")
    private boolean credentialsExpired;

	/**
	 * 
	 */
	public void recordTranfer() {

	}

	public Long getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Long employeeId) {
		this.employeeId = employeeId;
	}

	public String getEmployeeCode() {
		return employeeCode;
	}

	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getEmployDate() {
		return employDate;
	}

	public void setEmployDate(Date employDate) {
		this.employDate = employDate;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<Role> roleSet) {
		this.roleSet = roleSet;
	}
	
	public void addRole(Role role) {
		roleSet.add(role);
	}

	public Set<TransferRecord> getTransferRecordSet() {
		return transferRecordSet;
	}

	public void setTransferRecordSet(Set<TransferRecord> transferRecordSet) {
		this.transferRecordSet = transferRecordSet;
	}

	// -- Spring Security 框架的 UserDetails接口方法的实现： --
	//	@Transient
	//	public GrantedAuthority[] getAuthorities() {
	//		List<GrantedAuthority> resultList = new ArrayList<GrantedAuthority>();
	//		for(Iterator<Role> iter = getRoleSet().iterator(); iter.hasNext();) {
	//			Role role = iter.next();
	//			GrantedAuthorityImpl ga = new GrantedAuthorityImpl(role.getRoleCode());
	//			resultList.add(ga);
	//		}
	//		return resultList.toArray(new GrantedAuthority[resultList.size()]);
	//	}

	public boolean isEnabled() {
		return enabled;
	}

	public boolean isAccountExpired() {
		return accountExpired;
	}

	public void setAccountExpired(boolean accountExpired) {
		this.accountExpired = accountExpired;
	}

	public boolean isAccountLocked() {
		return accountLocked;
	}

	public void setAccountLocked(boolean accountLocked) {
		this.accountLocked = accountLocked;
	}

	public boolean isCredentialsExpired() {
		return credentialsExpired;
	}

	public void setCredentialsExpired(boolean credentialsExpired) {
		this.credentialsExpired = credentialsExpired;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public String getAccountPassword() {
		return accountPassword;
	}

	public void setAccountPassword(String accountPassword) {
		this.accountPassword = accountPassword;
	}

	@Override
	@Transient
	public String getPropertyNameOfId() {
		return "employeeId";
	}
}
